package com.example.order.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.order.domain.Orders;
import com.example.order.domain.User;
import com.example.order.service.OrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * @author: 不是菜狗爱编程
 * @date: 2024/04/04/9:13
 * @description:
 */
@RestController
@RequestMapping("order")
public class OrdersController {
    @Autowired
    private OrdersService ordersService;
    @Autowired
    private RestTemplate restTemplate;

    @PostMapping
    public Boolean save(@RequestBody Orders orders) {
        return ordersService.saveOrUpdate(orders);
    }

    @DeleteMapping("/{id}")
    public Boolean delete(@PathVariable Integer id) {
        return ordersService.removeById(id);
    }

    @GetMapping
    public List<Orders> findAll() {
        return ordersService.list();
    }

    @GetMapping("/{id}")
    public Orders findOne(@PathVariable Integer id) {
        Orders orders = ordersService.getById(id);
        User user = restTemplate.getForObject("http://user-service/user/" + orders.getUserId(), User.class);
        orders.setUser(user);
        return orders;
    }

    @GetMapping("/page")
    public Page<Orders> findPage(@RequestParam Integer pageNum,
                                @RequestParam Integer pageSize) {
        return ordersService.page(new Page<>(pageNum, pageSize));
    }
}
